package com.sw.c.controller;

import com.sw.c.service.FileInfoService;
import com.sw.c.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/file")
@Api(tags = "02.文件管理")
@Slf4j
public class FileController {

    @Resource
    private FileInfoService fileInfoService;


    @ApiOperation("文件上传")
    @PostMapping("/upload")
        public R<String> fileUpload(MultipartFile file,Integer userId) {
        String yesOrNo = fileInfoService.upload(file,userId);
        if ("上传成功".equals(yesOrNo)) {
            return R.success(yesOrNo);
        } else if ("上传失败".equals(yesOrNo)) {
            return R.error(yesOrNo);
        } else {
            return R.format(yesOrNo);
        }
    }

    @ApiOperation("文件下载")
    @GetMapping("/download")
    public void downloadFile(Integer fileId, HttpServletResponse response) {
        fileInfoService.fileDownload(fileId, response);
    }
}

